import 'package:cbec_app/components/base/base_function.dart';
import 'package:cbec_app/components/base/base_image.dart';
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/base/image_text_item.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/common/team_work_tag_item.dart';
import 'package:cbec_app/components/goods/goods_add_minus.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/EventMgr.dart';
import 'package:cbec_app/utils/ThemeConfig.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

///拼团弹框
class TeamWorkPopup extends StatefulWidget {

  ///商品信息
  final Map<String, dynamic> goodsInfo;

  ///点击确定按钮回调
  final CallBackInfo? onConfirm;




  const TeamWorkPopup(
      {super.key,
      this.onConfirm,
      required this.goodsInfo});

  @override
  State<StatefulWidget> createState() {
    return _TeamWorkPopupState();
  }
}

class _TeamWorkPopupState extends State<TeamWorkPopup> {
  ///当前选中的索引
  int curIndex=0;
  ///商品信息
  Map<String, dynamic> _goodsInfo={};

  ///已选商品数量
  int selectedGoodsNum = 1;
  @override
  void initState() {
    super.initState();
    _goodsInfo=widget.goodsInfo;
  }
  ///获取拼团列表
  List getTeamWorkList(){
    List arr=[];
    if(_goodsInfo['bookInfo']!=null&&_goodsInfo['bookInfo']['priceList']!=null){
      for(var item in _goodsInfo['bookInfo']['priceList']){
        arr.add(item);
      }
    }
    return arr;
  }
  ///获取拼团价
  

  @override
  void dispose() {
    super.dispose();
    EventMgr.off(EventType.refreshSkuPopup);
  }
  ///获取商品规格图片
  String getImgUrl() {
    return (_goodsInfo['imageList'] as List)[0];
  }
  ///获取商品价格
  String getGoodsPrice(){
    return getTeamWorkList()[curIndex]['priceBook'].toString();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
        height: MediaQuery.of(context).size.height * 0.7,
        decoration: const BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.only(
                topLeft: Radius.circular(8), topRight: Radius.circular(8))),
        child: Container(
          padding: EdgeInsets.only(top: 18.h, left: 18.w, right: 18.w),
          child: Stack(
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.end,
                        children: [
                          _goodsImg(radius: 0, left: 0),
                          Container(
                            margin: EdgeInsets.only(left: 10.w),
                            child: Column(
                              mainAxisAlignment: MainAxisAlignment.end,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                
                                goodsSalesPrice(
                                    getGoodsPrice(),
                                    fontSize: 15),
                                
                              ],
                            ),
                          )
                        ],
                      ),
                      BaseInteractive(
                        childWidget: Icon(Icons.close),
                        onClick: () {
                          Get.back();
                        },
                      )
                    ],
                  ),
                  SingleChildScrollView(
                    child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: createSkuList()),
                  ),
                ],
              ),
              Positioned(
                  bottom: MediaQuery.of(context).padding.bottom,
                  child: solidButton('确定', () {
                    Get.back();
                    if (widget.onConfirm != null) {
                      widget.onConfirm!(getTeamWorkList()[curIndex],0);
                    }
                  }))
            ],
          ),
        ));
  }

  

  ///生成规格列表
  List<Widget> createSkuList() {
    List<Widget> arr = [];
    arr.add(Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Container(
            margin: EdgeInsets.only(top: 20.h, bottom: 12.h),
            child: Text(
              '选择拼团数',
              style:
                  setTextStyle(13, FontWeight.w400, ThemeConfig.mainTextColor),
            ),
          ),
          TeamWorkTagItem(
            tagList: getTeamWorkList(),
            curIndex: 0,
            decoration: BoxDecoration(
                color: const Color(0xfff9f9f9),
                border: Border.all(color: Colors.transparent),
                borderRadius: const BorderRadius.all(Radius.circular(25))),
            activeDecoration: BoxDecoration(
                color: const Color(0xffF7D8D5),
                border: Border.all(color: ThemeConfig.themeColor),
                borderRadius: const BorderRadius.all(Radius.circular(25))),
            activeColor: ThemeConfig.themeColor,
            onClick: (index) {
              setState(() {
                curIndex=index;
                getGoodsPrice();
              });
            },
          )
        ],
      ));
    return arr;
  }
  ///商品图片
  Widget _goodsImg({double radius = 12, double? left}) {
    return Container(
      margin: EdgeInsets.only(left: left ?? 10.w),
      child: BaseImage(
        url: getImgUrl(),
        width: 100.w,
        height: 100.w,
        borderRadius: BorderRadius.all(Radius.circular(radius)),
      ),
    );
  }
}

///打开拼团弹框
///goodsInfo 商品信息
///selectedList 已选规格列表,可不传
///onConfirm 确定选择的规格
///onSelectSku 切换规格回调
void showTeamWorkPopup(
    {
    CallBackInfo? onConfirm,
    required Map<String, dynamic> goodsInfo}) {
  showCupertinoModalPopup(
    context: Get.context!,
    builder: (context) {
      return StatefulBuilder(
        builder: (context, setState) {
          return TeamWorkPopup(
              goodsInfo: goodsInfo,
              onConfirm: onConfirm,);
        },
      );
    },
  );
}
